from django.db import models

from UiManager.managers import PageInfoManager, ProjectInfoManager, TestCaseInfoManager, \
    TestStepInfoManager, ElementInfoManager, EnvInfoManager, LocationInfoManager, OprationInfoManager, \
    TestSuiteInfoManager, ResourceInfoManager, ReportInfoManager, LogInfoManager, SystemSettingsManager


# Create your models here.


class BaseTable(models.Model):
    create_time = models.DateTimeField('创建时间', auto_now_add=True)
    update_time = models.DateTimeField('更新时间', auto_now=True)
    simple_desc = models.CharField('简要描述', max_length=100, null=True)

    class Meta:
        abstract = True
        verbose_name = "公共字段表"
        db_table = 'BaseTable'
        app_label = "UiManager"


class ProjectInfo(BaseTable):
    class Meta:
        verbose_name = '项目信息'
        db_table = 'ProjectInfo'
        app_label = "UiManager"

    project_name = models.CharField('项目名称', max_length=50, unique=True, null=False)
    objects = ProjectInfoManager()


class PageInfo(BaseTable):
    class Meta:
        verbose_name = '页面信息'
        db_table = 'PageInfo'
        app_label = "UiManager"

    page_name = models.CharField('页面名称', max_length=50, null=False)
    belong_project = models.ForeignKey(ProjectInfo, on_delete=models.PROTECT)
    objects = PageInfoManager()


class LocationInfo(BaseTable):
    class Meta:
        verbose_name = '元素定位类型'
        db_table = 'LocationInfo'
        app_label = "UiManager"

    location_type = models.CharField('类型名称', max_length=50, null=False)
    objects = LocationInfoManager()


class ElementInfo(BaseTable):
    class Meta:
        verbose_name = '元素信息'
        db_table = 'ElementInfo'
        app_label = "UiManager"

    element_name = models.CharField('元素名称', max_length=50, null=False)
    belong_location = models.ForeignKey(LocationInfo, on_delete=models.PROTECT, null=True)
    element_value = models.CharField('元素文本', max_length=50, null=False)
    belong_page = models.ForeignKey(PageInfo, on_delete=models.PROTECT, null=True)
    objects = ElementInfoManager()


class VariableInfo(BaseTable):
    class Meta:
        verbose_name = '参数信息'
        db_table = 'VariableInfo'
        app_label = "UiManager"

    variable_name = models.CharField('变量名称', max_length=50, null=False)
    variable_key = models.CharField('变量引用', max_length=50, null=False)
    variable_value = models.CharField('变量值', max_length=50, null=False)


class HooksInfo(BaseTable):
    class Meta:
        verbose_name = '钩子信息'
        db_table = 'HooksInfo'
        app_label = "UiManager"

    hooks_name = models.CharField('钩子名称', max_length=50, null=False)
    hooks_type = models.IntegerField('钩子类型', null=False)   # 0:setup 1:teardown
    belong_project = models.ForeignKey(ProjectInfo, on_delete=models.PROTECT)


class OprationInfo(BaseTable):
    class Meta:
        verbose_name = '操作方式'
        db_table = 'OprationInfo'
        app_label = "UiManager"

    opration_name = models.CharField('定位方式名称', max_length=50, null=False)
    objects = OprationInfoManager()


class TestStepInfo(BaseTable):
    class Meta:
        verbose_name = '步骤信息'
        db_table = 'TestStepInfo'
        app_label = "UiManager"

    step_name = models.CharField('步骤名称', max_length=50, null=False)
    belong_element = models.ForeignKey(ElementInfo, on_delete=models.PROTECT, null=True)
    belong_opration = models.ForeignKey(OprationInfo, on_delete=models.PROTECT)
    input_data = models.CharField('输入数据', max_length=100, null=True)
    step_must = models.CharField('必要步骤', max_length=50, null=True)

    objects = TestStepInfoManager()


class TestCaseInfo(BaseTable):
    class Meta:
        verbose_name = '用例信息'
        db_table = 'TestCaseInfo'
        app_label = "UiManager"

    case_name = models.CharField('用例名称', max_length=50, null=False)
    belong_project = models.ForeignKey(ProjectInfo, on_delete=models.PROTECT)
    steps = models.CharField('步骤集合', max_length=500, null=False)
    hooks = models.CharField('钩子方法', max_length=100, null=True)

    objects = TestCaseInfoManager()


class TestSuiteInfo(BaseTable):
    class Meta:
        verbose_name = "测试套件"
        db_table = 'TestSuiteInfo'
        app_label = "UiManager"

    suite_name = models.CharField(max_length=50, null=False)
    duration = models.CharField(max_length=50, null=True)
    cases = models.CharField(max_length=500, null=False)
    belong_project = models.ForeignKey(ProjectInfo, on_delete=models.PROTECT)
    status = models.CharField(max_length=50, null=False)

    objects = TestSuiteInfoManager()


class EnvInfo(BaseTable):
    class Meta:
        verbose_name = '环境管理'
        db_table = 'EnvInfo'
        app_label = "UiManager"

    env_name = models.CharField(max_length=50, null=False)
    base_url = models.CharField(max_length=50, null=False)
    capabilities = models.CharField(max_length=2000, null=False)
    rule = models.CharField(max_length=150, null=True)
    objects = EnvInfoManager()


class ResourceInfo(BaseTable):
    class Meta:
        verbose_name = 'resource管理'
        db_table = 'ResourceInfo'
        app_label = "UiManager"

    resource_name = models.CharField(max_length=50, null=False)
    objects = ResourceInfoManager()


class DeviceInfo(BaseTable):
    class Meta:
        verbose_name = '设备管理'
        db_table = 'DeviceInfo'
        app_label = "UiManager"
    p_id = models.AutoField(primary_key=True)
    deviceid = models.CharField(max_length=100)
    port = models.CharField(max_length=100)
    bport = models.CharField(max_length=100)
    aport = models.CharField(max_length=100)
    isused = models.CharField(max_length=100)
    tag = models.CharField(max_length=100)


class ReportInfo(BaseTable):
    class Meta:
        verbose_name = '测试报告'
        db_table = 'ReportInfo'
        app_label = 'UiManager'

    html_report_name = models.CharField(max_length=50, null=False)
    status = models.CharField(max_length=50, null=True)
    successes = models.CharField(max_length=50, null=True)
    testsRun = models.CharField(max_length=50, null=True)
    failures = models.CharField(max_length=50, null=True)
    percent = models.CharField(max_length=50, null=True)
    start_at = models.CharField(max_length=50, null=True)
    report_path = models.CharField(max_length=100, null=True)

    objects = ReportInfoManager()


class LogInfo(BaseTable):
    class Meta:
        verbose_name = '运行日志'
        db_table = 'LogInfo'
        app_label = 'UiManager'

    log_name = models.CharField(max_length=50, null=False)
    belong_suite = models.ForeignKey(TestSuiteInfo, on_delete=models.PROTECT)

    objects = LogInfoManager()


class SystemSettings(BaseTable):
    class Meta:
        verbose_name = '系统设置'
        db_table = 'SystemSettings'
        app_label = 'UiManager'

    main_dir = models.CharField(max_length=300, null=False)
    server = models.GenericIPAddressField(max_length=50, null=True, default='127.0.0.1')
    log_dir = models.CharField(max_length=300, null=False)
    image_dir = models.CharField(max_length=300, null=False)
    resource_dir = models.CharField(max_length=300, null=False)
    rabbitMq_url = models.CharField(max_length=300, null=True, default='amqp://guest:guest@127.0.0.1:5672//')
    email_user = models.EmailField(max_length=300, null=False)
    email_password = models.CharField(max_length=300, null=False)

    objects = SystemSettingsManager()
